﻿using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FBGraph.ClientGenerator
{
    /// <summary>Extension methods that support changing the case of strings</summary>
    public static class StringExtensions
    {
        private static String ToCase(this String value, Boolean capitalizeFirst)
        {
            if (value.IndexOfAny("0123456789".ToCharArray()) >= 0)
            {
                return Regex.Replace(value, @"\b(?:(?<split>[-_0-9\\/\w])*(?<cap>[A-Za-z])(?<rest>[A-Za-z]*))*", new MatchEvaluator(match =>
                {
                    var b = new StringBuilder();
                    for (var i = 0; i < match.Groups["cap"].Captures.Count; i++)
                    {
                        var first = match.Groups["cap"].Captures[i].Value;
                        if (i > 0 || capitalizeFirst) b.Append(first.ToUpper());
                        else b.Append(first);
                        if (match.Groups["rest"].Captures.Count > i) b.Append(match.Groups["rest"].Captures[i].Value);
                        if (match.Groups["split"].Captures.Count > i && Regex.IsMatch(match.Groups["split"].Captures[i].Value, @"[0-9\w]")) b.Append(match.Groups["split"].Captures[i].Value);
                    }
                    return b.ToString();
                }));
            }
            else
            {
                var result = String.Join(String.Empty, value.Split("-_\\/ ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(s => s.ToCharArray()[0].ToString().ToUpper() + s.Substring(1)).ToArray());
                if (capitalizeFirst) return result;
                else return result.ToCharArray()[0].ToString().ToLower() + result.Substring(1);
            }
        }

        public static String ToPascalCase(this String value)
        {
            return value.ToCase(true);
        }

        public static String ToCamelCase(this String value)
        {
            return value.ToCase(false);
        }

        public static String CapitalizeFirst(this String value)
        {
            return value.Substring(0, 1).ToUpper() + value.Substring(1);
        }

        public static String ToClassName(this String value)
        {
            if (value == "Link") return "Post";
            else return value.ToPascalCase();
        }

        public static String Depluralize(this String value)
        {
            if (new String[] { "Movies", "Music", "Books", "Television", "Interests", "Activities", "Likes" }.Contains(value)) return "Page";            
            else if (new String[] { "Inbox", "Outbox", "Updates" }.Contains(value)) return "MailboxItem";
            else if (new String[] { "Feed", "Home", "Links", "Tagged" }.Contains(value)) return "Post";
            else if (new String[] { "Attending", "Declined", "Invited", "Maybe", "Noreply" }.Contains(value)) return "EventRsvp";
            else if (new String[] { "Users", "Friends" }.Contains(value)) return "User";
            else if (value == "Statuses") return "StatusMessage";
            else if (value == "Noreply") return "NoReply";
            else if (value.EndsWith("ies")) return value.Substring(0, value.Length - 3);
            else return value.TrimEnd('s');
        }
    }
}